package fr.esiag.isidis.epp.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;

import org.apache.log4j.Logger;

public class Utils {

	private static Logger logger = Logger.getLogger(Utils.class);
	
	public static int convertDateToInt(String date){
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm");
		try {
			calendar.setTime(format.parse(date));
		} catch (ParseException e) {
			logger.error("date format does not conform");
		}

		int year       = calendar.get(Calendar.YEAR);
		int month      = calendar.get(Calendar.MONTH) + 1; // jan = 0 +1 = 1
		int day = calendar.get(Calendar.DAY_OF_MONTH); 
		
		//ex : 20141021 = date 21/10/2014
		return year*10000+month*100+day;
		
	}
	
	public static int convertDateToIntSite(String date){
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
		try {
			calendar.setTime(format.parse(date));
		} catch (ParseException e) {
			logger.error("date format does not conform");
		}

		int year       = calendar.get(Calendar.YEAR);
		int month      = calendar.get(Calendar.MONTH) + 1; // jan = 0 +1 = 1
		int day = calendar.get(Calendar.DAY_OF_MONTH); 
		
		//ex : 20141021 = date 21/10/2014
		return year*10000+month*100+day;
		
	}
	
	
	public static int convertHourToInt(String date){
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm");
		try {
			calendar.setTime(format.parse(date));
		} catch (ParseException e) {
			logger.error("date format does not conform");
		}

		int hour  = calendar.get(Calendar.HOUR_OF_DAY); // 24 hour clock
		int minute     = calendar.get(Calendar.MINUTE);
	//	int second     = calendar.get(Calendar.SECOND);

		//ex : 1210 => 20h 10mn 00s
		return hour*60+minute;
	}
	
	public static String getIdnn(int id){
		id = id+1;
		if (id == 1) return ("ID01");
		if (id == 2) return ("ID02");
		if (id == 3) return ("ID03");
		if (id == 4) return ("ID04");
		if (id == 5) return ("ID05");
		if (id == 6) return ("ID06");
		if (id == 7) return ("ID07");
		if (id == 8) return ("ID08");
		if (id == 9) return ("ID09");
		else return ("ID"+id);
	}
	
	// methode pour tranformer id01 en idsite1
	public static String transformeIdnn(String id){
		String res;
		String sufix = id.substring(2, id.length());
		if(sufix.equals("01")){res = "IDSITE1"; return res;}
		if(sufix.equals("02")){res = "IDSITE2"; return res;}
		if(sufix.equals("03")){ res = "IDSITE3"; return res;}
		if(sufix.equals("04")){ res = "IDSITE4"; return res;}
		if(sufix.equals("05")){res = "IDSITE5"; return res;}
		if(sufix.equals("06")){res = "IDSITE6"; return res;}
		if(sufix.equals("07")){res = "IDSITE7"; return res;}
		if(sufix.equals("08")){res = "IDSITE8"; return res;}
		if(sufix.equals("09")){res = "IDSITE9"; return res;}
		else
		res = id.replace("ID", "IDSITE");
		return res;
		
	}
	
	// methode pour tranformer idsite1 en id01
		public static String transformeIdSite(String id){
			String res;
			String sufix = id.substring(6, id.length());
			if(sufix.equals("1")){res = "ID01"; return res;}
			if(sufix.equals("2")){res = "ID02"; return res;}
			if(sufix.equals("3")){ res = "ID03"; return res;}
			if(sufix.equals("4")){ res = "ID04"; return res;}
			if(sufix.equals("5")){res = "ID05"; return res;}
			if(sufix.equals("6")){res = "ID06"; return res;}
			if(sufix.equals("7")){res = "ID07"; return res;}
			if(sufix.equals("8")){res = "ID08"; return res;}
			if(sufix.equals("9")){res = "ID09"; return res;}
			else
			res = id.replace("IDSITE", "ID");
			return res;
			
		}
	public static String virguleTopoint(String a){
		if(a.contains(",")){
		String[] temp = a.split(",");
		a = temp[0]+"."+temp[1];
		return a;
		}else
			return a;
	}
	
	public static String makedate(int date, int heure){
		String dateI = Integer.toString(date);
		String dd = dateI.substring(6,8);
		String MM = dateI.substring(4,6);
		String yyyy = dateI.substring(0, 4);
		String mm = Utils.intToString(heure % 60);
		String hh = Utils.intToString(heure / 60);
		String format = (dd+"/"+MM+"/"+yyyy+" "+hh+":"+mm);
		return format;
	}
	
	public static String makedateOnly(int date) {
		String dateI = Integer.toString(date);
		String dd = dateI.substring(6,8);
		String MM = dateI.substring(4,6);
		String yyyy = dateI.substring(0, 4);
		String format = (dd+"/"+MM+"/"+yyyy);
		return format;
	}
	
	public static String intToString(int nombre) {
		String n;
		if(nombre < 10) {
			 n = "0";
			n += Integer.toString(nombre);
			return n;
		}
		else 
			n =Integer.toString(nombre); 
			return n;
	}
   public static void main( String[] args ){
	   int date = 20140911;
	   int heure = 1265;
	  String dateI =  makedate(date, heure);
	  String dateo = makedateOnly(date);
	  System.out.println(dateo);
//    	System.out.println("entre un signe");
//    	Scanner sc = new Scanner(System.in);
//    	String a = sc.nextLine();
//    	String b = a.substring(2, a.length());
//    	a = Utils.transformeIdnn(a);
//    	System.out.println(a);
//    	System.out.println(b);
   }
}
